package com.scpii.universal.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.scpii.universal.hibernate.dao.BaseDao;
import com.scpii.universal.hibernate.entity.CatelogTab;
import com.scpii.universal.hibernate.intface.BaseInterface;
import com.scpii.universal.util.Const;

public class CatelogTabService implements BaseInterface<CatelogTab> {
	private static final Logger log = LoggerFactory
			.getLogger(CatelogTabService.class);
//	private BaseDao<CatelogTab> catelogTabDao = SpringBeanFactory.catelogTabDao;
	private BaseDao<CatelogTab> catelogTabDao;
	
	public CatelogTab get(Integer id) throws Exception {
		return catelogTabDao.get(id);
	}

	public List<CatelogTab> getAll() throws Exception {
		return catelogTabDao.getAll();
	}

	public CatelogTab get(String propertyName, Object value) throws Exception {
		return null;
	}

	public List<CatelogTab> getList(String propertyName, Object value)
			throws Exception {
		return null;
	}

	public Long getTotalCount() throws Exception {
		return null;
	}

	public void save(CatelogTab obj) throws Exception {

	}

	public void update(CatelogTab obj) throws Exception {

	}

	public void delete(CatelogTab obj) throws Exception {

	}

	public void delete(Integer id) throws Exception {

	}

	public List<CatelogTab> hqlGetList(String hql, Map<String, Object> argsMap)
			throws Exception {
		return catelogTabDao.hqlGetList(hql, argsMap);
	}

	public void hqlUpdate(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		catelogTabDao.hqlUpdate(namedHqlQuery, argsMap);
	}

	public Long hqlGetNums(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		return catelogTabDao.hqlGetNums(namedHqlQuery, argsMap);
	}

	public List<CatelogTab> sqlGetList(String sqls, Map<String, Object> argsMap)
			throws Exception {
		return catelogTabDao.sqlGetList(sqls, argsMap);
	}

	public void sqlUpdate(String sqls, Map<String, Object> argsMap)
			throws Exception {
		catelogTabDao.sqlUpdate(sqls, argsMap);
	}

	public Long sqlGetNum(String sqls, Map<String, Object> argsMap)
			throws Exception {
		return catelogTabDao.sqlGetNum(sqls, argsMap);
	}

	/**
	 * 根据应用父ID，获得父分类下的子分类列表
	 * 
	 * @param appId
	 * @param catelogId
	 * @return
	 * @throws Exception
	 * @author 甘建新 2012-7-17上午02:10:38
	 */
	public List<CatelogTab> getCatelogListByParentCatelogId(int appId,
			int catelogId) throws Exception {
		List<CatelogTab> cateList = new ArrayList<CatelogTab>();
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("appId", appId);
		argsMap.put("catelogId", catelogId);
		cateList = this.hqlGetList("GET_SUB_CATELOGLIST_BY_APPID_CATEID", argsMap);
		return cateList;
	}
	
	/**
	 * 通过递归查询取得最顶级的分类信息
	 * @param cateId
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-7-26上午12:28:29
	 */
	public CatelogTab getSuperCatelogByCateId(int cateId)throws Exception{
		if (cateId>0) {
			CatelogTab catelogTab = this.get(cateId);
			if (catelogTab!=null) {
				if (catelogTab.getIntParentCateId()!=0) {
					catelogTab = getSuperCatelogByCateId(catelogTab.getIntParentCateId());
					return catelogTab;
				}else {
					return catelogTab;
				}
			}
		}
		return null;
	}
	
	/**
	 * 根据分类ID和应用ID查询所有子分类
	 * @author Taylor
	 * 2012-9-19 下午06:25:30
	 * @param id
	 * @return
	 * List<CatelogTab>
	 */
	public List<CatelogTab> getSubordinateCatelogs(int id) {
		return catelogTabDao.getSubordinateCatelogs(id);
	}
	
	/**
	 * 获取应用房产类的根分类ID
	 * @author Taylor
	 * 2012-9-19 下午07:01:29
	 * @param appId
	 * @return
	 * CatelogTab
	 * @throws Exception 
	 */
	public int getHouseSuperCatelogId(int appId) throws Exception {
		List<CatelogTab> cateList = new ArrayList<CatelogTab>();
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("appId", appId);
		argsMap.put("intType", Const.CATELOG_TYPE_HOUSE);
		cateList = this.hqlGetList("GET_SUPER_CATELOG_BY_APPID", argsMap);
		if (cateList != null && cateList.size() > 0) {
			return cateList.get(0).getIntCateId();
		}
		return 0;
	}
	
	public static void main(String[] args) {
		try {
//			CatelogTab catelogTab = (new CatelogTabService()).getSuperCatelogByCateId(4);
//			System.out.println(catelogTab.getVarCateName()+":"+catelogTab.getIntParentCateId());
			
			List<CatelogTab> cateList =(new CatelogTabService()).getCatelogListByParentCatelogId(20120960, 9);
			System.out.println(cateList.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//Setter

	public void setCatelogTabDao(BaseDao<CatelogTab> catelogTabDao) {
		this.catelogTabDao = catelogTabDao;
	}
	
}